package day7;

import java.util.Arrays;
import java.util.Scanner;

import org.junit.Test;

public class StringDemo {

	@Test
	public void test1() {
		String str1 = "Hello";
		String str2 = new String("Hello");
		char[] array = str1.toCharArray();
		// ['H', 'e', 'l', 'l', 'o']
		char ch = str1.charAt(2);
		System.out.println(ch);// l
	}
	
	@Test
	public void test2() {
		// "abc"字符串常量对象
		String str1 = "abc";
		String str2 = new String("abc");
		String str3 = "abc";
		String str4 = new String("abc");
		
		// == 是判断地址是不是相等
		System.out.println(str1 == str2);// false
		System.out.println(str1 == str3);// true
		System.out.println(str2 == str3);// false
		System.out.println(str2 == str4);

		// equal 是判断内容是不是相等
		System.out.println(str1.equals(str2));// true
		System.out.println(str1.equals(str3));// true
		System.out.println(str2.equals(str3));// true
	}
	
	@Test
	public void test3() {
		String str = "java AndroidA";
		System.out.println(str.length());
		char[] array = str.toCharArray();
		char ch = str.charAt(2);
		System.out.println(ch);// v
		
		System.out.println(str.indexOf('A'));// 5
		// 在此对象表示的字符序列中第一次出现该字符的索引；如果未出现该字符，则返回 -1。
		System.out.println(str.indexOf('X'));// -1
		System.out.println(str.indexOf('A', 6));// 12
		System.out.println(str.indexOf("And"));// 5
		
		System.out.println(str.lastIndexOf('A'));// 12
	}
	
	@Test
	public void test4() {
		String str1 = "Java";
		String str2 = "JAva";
		// case sensitive
		System.out.println(str1.equals(str2));// false
		System.out.println(str1.equalsIgnoreCase(str2));// true
		
		String str = "java AndroidA";
		System.out.println(str.contains("And"));// true
		System.out.println(str.startsWith("java A"));// true
		// offset:偏移
		System.out.println(str.startsWith("And", 5));//true
		System.out.println(str.endsWith("idA"));// true
		System.out.println(str.isEmpty());// false
		System.out.println("".isEmpty());// true
		char ch = ' ';// 空格字符
		String s = "";
	}
	
	@Test
	public void test5() {
		char[] array = {'j', 'a', 'v', 'a', ' ', 'A', 'n', 'd'};
		String str = new String(array);
		System.out.println(str);// java And
		char[] charArray = str.toCharArray();
		for (int i = 0; i < charArray.length; i++) {
			System.out.print(charArray[i] + "  ");
		}
		System.out.println();
		System.out.println(Arrays.toString(charArray));
	}
	
	@Test
	public void test6() {
		String str = "java AndroidA";
		String newStr = str.replace('A', 'B');
		System.out.println(str);// java AndroidA
		System.out.println(newStr);// java BndroidB
	}
	
	@Test
	public void test7() {
		// 4-8   字母数字下划线
		String str = "java Android Python";
		String[] array = str.split(" ");
		for (int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}
	}
	
	@Test
	public void test8() {
		String str = "java AndroidA";
		System.out.println(str.substring(5));// AndroidA
		// beginIndex the beginning index, inclusive.
		// endIndex the ending index, exclusive.
		System.out.println(str.substring(5, 8));// And
		// str.截取子字符串(开始的索引，结束的索引)
	}
	
	@Test
	public void test9() {
		String str = "    java AndroidA    ";
		System.out.println(str);//     java AndroidA    
		System.out.println(str.trim());// java AndroidA
	}
	
	@Test
	public void test10() {
		// 实现trim方法
//		Scanner scanner = new Scanner(System.in);
//		System.out.println("请输入字符串");
//		String str = scanner.nextLine();
//		scanner.close();
		
		
		String str = "---";
		int start = 0;
		int end = str.length() - 1;
		char[] array = str.toCharArray();
		while ((start <= end) && (array[start] == '-')) {
			start++;
		}
		while ((start <= end) && (array[end] == '-')) {
			end--;
		}
		String newstStr = str.substring(start, end + 1);
		System.out.println(newstStr);
	}
	
	@Test
	public void test66() {
		String str = "---";
		int start = 0;
		int end = str.length() - 1;
		while ((start <= end) && (str.charAt(start) == '-')) {
			start++;
		}
		while ((start <= end) && (str.charAt(end) == '-')) {
			end--;
		}
		String newstStr = str.substring(start, end + 1);
		System.out.println(newstStr);
	}
	
	@Test
	public void test44() {
		int num1 = 3;
		int num2 = 5;
		int max = getMax(num1, num2);
		System.out.println(max);
		
		int m = Math.max(3, 5);
	}

	private int getMax(int num1, int num2) {
//		if (num1 >= num2) {
//			return num1;
//		} else {
//			return num2;
//		}
		// 三目运算符
		return (num1 >= num2) ? num1 : num2;
	}
	
	
	@Test
	public void test616() {
		String str1 = "Java";
		String str2 = "Andorid";
		String str3 = "Python";
		
		//str1 = str1 + str2 + str3;
		//System.out.println(str1);
		
		StringBuffer buffer = new StringBuffer(str1);
		buffer.append(str2);
		buffer.append(str3);
		System.out.println(buffer);
		
		StringBuilder builder = new StringBuilder();
		// 链式
		builder.append(str1).append(str2).append(str3);
	}
	
}
